#!/usr/bin/env python
#! coding:utf-8


import smtplib
from email.mime.text import MIMEText
from email.header import Header
from crypt import decrypt
import json
import sqlite3

class QQEmail():

    def __init__(self, check_rslt):

        self.con = sqlite3.connect('infec.db')
        self.cur = self.con.cursor()
        self.cur.execute('select * from qq where id=1')
        qq_rslt = self.cur.fetchone()
        qq_rslt1 = decrypt(qq_rslt[1])
        qq_rslt2 = decrypt(qq_rslt[2])
        self.con.close()
        self.msg_from = qq_rslt1
        self.passwd = qq_rslt2
        self.msg_to = qq_rslt1
        msg = '审卡结果:'
        for k in check_rslt:
            msg += "{}:{};\n".format(k,check_rslt[k])
        self.check_rslt = msg
        print(self.msg_from, self.msg_to,self.check_rslt)
        self.send()

    def send(self):
        msg = MIMEText(self.check_rslt)
        msg['Content-Type'] = 'Text/HTML'
        msg['Subject'] = Header('log','gb2312')
        msg['From'] = self.msg_from
        msg['To'] = self.msg_to

        s = smtplib.SMTP_SSL('smtp.qq.com',465)
        try:
            s.login(self.msg_from,self.passwd)
            print('loging')
            s.sendmail(self.msg_from,self.msg_to,msg.as_string())
            print("emit success")
        except:
            print("emit error")
        finally:
            s.quit()


if __name__ == "__main__":
    b = {'a':1,"b":2}
    a = QQEmail(b)
